<template>
  <div>
    <el-card>
      <el-form ref="form" :model="teacher" label-width="80px" style="width: 50%">
        <el-form-item label="讲师姓名">
          <el-input v-model="teacher.name"></el-input>
        </el-form-item>
        <el-form-item label="讲师资历">
          <el-input v-model="teacher.career" placeholder="一句话说明讲师"></el-input>
        </el-form-item>
        <el-form-item label="讲师头衔">
          <el-select v-model="teacher.level">
            <el-option label="高级讲师" :value="0"></el-option>
            <el-option label="首席讲师" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="讲师头像">
          <el-upload
            class="avatar-uploader"
            :action="BASE_API+'/oss/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload" list-type="picture-card">
            <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="讲师排序">
          <el-select v-model="teacher.sort">
            <el-option :label="num" :value="num" v-for="(num,index) in 5" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="讲师简介">
          <el-input type="textarea" :rows="20" v-model="teacher.intro"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button @click="onClean">清空</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

  import { getTeacherById, saveTeacher, updateTeacher } from '@/api/edu/teacher'

  export default {
    name: 'Save',
    data() {
      return {
        teacher: {
          id: '',
          name: '',
          intro: '',
          career: '',
          level: '',
          avatar: '',
          sort: ''
        },
        BASE_API: process.env.BASE_API
      }
    },
    created() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        getTeacherById(id)
          .then(res => {
            const { success, data, message } = res
            if (success) {
              this.teacher = { ...data }
            }
          })
      }
    },
    methods: {

      // 保存讲师信息
      onSubmit() {
        if (!this.teacher.id) {
          saveTeacher(this.teacher)
            .then(res => {
              const { success, message, data } = res
              if (success) {
                this.$message.success(message)
                this.$router.push('/teacher')
              }
            })
        } else {
          updateTeacher(this.teacher)
            .then(res => {
              const { success, message, data } = res
              if (success) {
                this.$message.success(message)
                this.$router.push('/teacher')
              }
            })
        }
      },
      // 清空讲师信息
      onClean() {
        this.teacher = {}
      },
      // 图像上传成功回调
      handleAvatarSuccess(res, file) {
        this.teacher.avatar = URL.createObjectURL(file.raw)
      },
      // 图像上传之前
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 2014 < 2
        if (!isJPG) {
          this.$message.error('上传头像图片格式错误！')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过2MB！')
        }
      }
    },
    watch: {
      $route(to, from) {
        console.log('路由变化了')
        this.teacher = {}
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
